
export default {
    canUndo: true,
    execute: (context, options, isUndo) => {
        const { sheet, selections } = options;
        sheet.suspendPaint();
        try {
            if (isUndo) {
                selections.forEach(detail => {
                    sheet.removeSpan(detail.row, detail.col);
                    for (let row = detail.row, i = 0; i < detail.rowCount; i++ , row++) {
                        for (let col = detail.col, j = 0; j < detail.colCount; col++ , j++) {
                            let cell;
                            const table = sheet.tables.find(row, col);
                            if (table) {
                                continue;
                                //cell = sheet.getCell(table.dataRange().row, col);
                            } else {
                                cell = sheet.getCell(row, col);
                            }
                            cell.cellType(options._oldCellTypes[row][col])
                                .backColor(options._oldBackColor[row][col])
                                .textIndent(options._oldTextIndent[row][col])
                                .backgroundImageLayout(options._oldBackgroundImageLayout[row][col])
                                .backgroundImage(options._oldBackGroundImage[row][col]);
                        }
                    }
                });
            } else {
                options._oldBackColor = {};
                options._oldCellTypes = {};
                options._oldBackGroundImage = {};
                options._oldBackgroundImageLayout = {};
                options._oldTextIndent = {};
                selections.forEach(detail => {
                    for (let row = detail.row, i = 0; i < detail.rowCount; i++ , row++) {
                        if (!options._oldBackColor[row]) {
                            options._oldBackColor[row] = {}
                        }
                        if (!options._oldCellTypes[row]) {
                            options._oldCellTypes[row] = {};
                        }
                        if (!options._oldTextIndent[row]) {
                            options._oldTextIndent[row] = {};
                        }
                        if (!options._oldBackGroundImage[row]) {
                            options._oldBackGroundImage[row] = {};
                        }
                        if (!options._oldBackgroundImageLayout[row]) {
                            options._oldBackgroundImageLayout[row] = {};
                        }
                        for (let col = detail.col, j = 0; j < detail.colCount; j++ , col++) {
                            let cell;
                            const table = sheet.tables.find(row, col);
                            if (table) {
                                continue;
                                //cell = sheet.getCell(table.dataRange().row, col);
                            } else {
                                cell = sheet.getCell(row, col);
                            }
                            options._oldBackColor[row][col] = cell.backColor();
                            options._oldCellTypes[row][col] = cell.cellType();
                            options._oldTextIndent[row][col] = cell.textIndent();
                            options._oldBackGroundImage[row][col] = cell.backgroundImage();
                            options._oldBackgroundImageLayout[row][col] = cell.backgroundImageLayout();

                            cell.cellType(undefined)
                                .backColor(undefined)
                                .textIndent(undefined)
                                .backgroundImageLayout(undefined)
                                .backgroundImage(undefined)
                        }
                    }
                });
            }
        } catch (e) {
            if (!!(window.console && window.console.log)) {
                window.console.log(e);
            }
        }
        sheet.resumePaint();
    }
};